package com.skb.core.dao;

import com.skb.core.model.Account;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.List;

@Repository("accountDao")
public class AccountDaoImpl implements AccountDao {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    public Account create(Account account) {
        sessionFactory.getCurrentSession().persist(account);
        return account;
    }

    @Override
    public Account get(Serializable id) {
        return (Account) sessionFactory.getCurrentSession().get(Account.class, id);
    }

    @Override
    public Account update(Account account) {
        sessionFactory.getCurrentSession().update(account);
        return account;
    }

    @Override
    public void delete(Account account) {
        sessionFactory.getCurrentSession().delete(account);
    }

    @Override
    public List<Account> findAll() {
        return sessionFactory.getCurrentSession().createQuery("from Account").list();
    }

    @Override
    public List<Account> findByUsername(Serializable username) {
        return sessionFactory.getCurrentSession().createQuery("from Account where user.username = ?").setParameter(0, username).list();
    }
}
